package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.WxComment;
import com.cskaoyan.bean.wx.WxOrder;
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.wx.WxCommentMapper;
import com.cskaoyan.mapper.wx.WxOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MallServiceImpl implements MallService {

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    WxOrderMapper wxOrderMapper;
    @Autowired
    WxCommentMapper wxCommentMapper;


    @Autowired
    CategoryIdMapper categoryMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    KeywordMapper keywordMapper;


    @Override
    public List<Region> queryRegions() {
        RegionExample regionExample1 = new RegionExample();
        RegionExample.Criteria criteria = regionExample1.createCriteria().andTypeEqualTo((byte) 1);
        List<Region> regions1 = regionMapper.selectByExample(regionExample1);

        RegionExample regionExample2 = new RegionExample();
        RegionExample.Criteria criteria2 = regionExample2.createCriteria().andTypeEqualTo((byte) 2);
        List<Region> regions2 = regionMapper.selectByExample(regionExample2);

        RegionExample regionExample3 = new RegionExample();
        RegionExample.Criteria criteria3 = regionExample3.createCriteria().andTypeEqualTo((byte) 3);
        List<Region> regions3 = regionMapper.selectByExample(regionExample3);


        for (Region r2 : regions2) {
            for (Region r3 : regions3) {
                if (r2.getCode() == r3.getCode() / 100) {
                    r2.getChildren().add(r3);
                }
            }
        }

        for (Region r1 : regions1) {
            for (Region r2 : regions2) {
                if (r1.getCode() == r2.getCode() / 100) {
                    r1.getChildren().add(r2);
                }
            }
        }

        return regions1;
    }

    @Override
    public BrandBean queryBrands(Integer id, String name, Integer page, Integer limit, String sort, String order) {

        //
        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page, limit);

        //example是提供条件的
        BrandExample brandExample = new BrandExample();
        //and + 列名 + 条件
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }

        //排序
        brandExample.setOrderByClause(sort + " " + order);
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(brands);
        long total = pageInfo.getTotal();

        BrandBean brandBean = new BrandBean();

        brandBean.setTotal(total);
        brandBean.setItems(brands);

        return brandBean;
    }

    @Override
    public ListBean orderList(Integer page, Integer limit,Short[] orderStatusArray,String sort, String order,Integer userId,String orderSn) {
        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page,limit);

        //example是提供条件的
        OrderExample orderExample = new OrderExample();
        //and + 列名 + 条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if(orderStatusArray != null){
            criteria.andOrderStatusIn(Arrays.asList(orderStatusArray));
        }
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null){
            criteria.andOrderSnLike(orderSn+"%");
        }

        //排序
        orderExample.setOrderByClause(sort + " " + order);

        List<Order> orders = orderMapper.selectByExample(orderExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(orders);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setTotal(total);
        listBean.setItems(orders);
        return listBean;
    }

    public void brandDelete(BrandBO brand) {
        brandMapper.deleteByPrimaryKey(brand.getId());
    }

    @Override
    public List<CategoryListVO> categoryList() {
        List<CategoryListVO> categoryListVOS = categoryMapper.selectCategoryListVO(0);
        for (CategoryListVO categoryListVO : categoryListVOS) {
            List<CategoryChildren> childrenList = categoryMapper.selectCategoryChildren(categoryListVO.getId());
            categoryListVO.setChildren(childrenList);
        }
        return categoryListVOS;
    }

    @Override
    public List<CategoryVO> categoryListL1() {
        List<CategoryVO> categoryVOList = categoryMapper.selectCategoryVO(0);
        return categoryVOList;
    }

    @Override
    public CategoryCreateVO create(CategoryChildren categoryCreate) {
        CategoryId categoryId = new CategoryId();
        categoryId.setAddTime(new Date());
        categoryId.setUpdateTime(new Date());
        categoryId.setName(categoryCreate.getName());
        categoryId.setDesc(categoryCreate.getDesc());
        categoryId.setIconUrl(categoryCreate.getIconUrl());
        categoryId.setKeywords(categoryCreate.getKeywords());
        categoryId.setPicUrl(categoryCreate.getPicUrl());
        categoryId.setPid(categoryCreate.getPid());
        categoryId.setLevel(categoryCreate.getLevel());
        categoryMapper.insertSelective(categoryId);
        Integer id = categoryId.getId();
        CategoryCreateVO categoryCreateVO = categoryMapper.selectCategoryCreateVO(id);
        return categoryCreateVO;
    }

    @Override
    public void delete(CategoryChildren categoryCreate) {
        categoryMapper.deleteByPrimaryKey(categoryCreate.getId());
        CategoryIdExample categoryIdExample = new CategoryIdExample();
        categoryIdExample.createCriteria().andPidEqualTo(categoryCreate.getId());
        categoryMapper.deleteByExample(categoryIdExample);
    }

    @Override
    public void update(CategoryId categoryUpdate) {
        CategoryIdExample categoryIdExample = new CategoryIdExample();
        categoryIdExample.createCriteria().andIdEqualTo(categoryUpdate.getId());
        categoryUpdate.setUpdateTime(new Date());
        categoryMapper.updateByExampleSelective(categoryUpdate, categoryIdExample);
    }

    @Override
    public BrandCreateVO brandCreate(String desc, String floorPrice, String name, String picUrl) {
        Brand brand = new Brand();
        brand.setName(name);
        brand.setDesc(desc);
        if (floorPrice != null) {
            brand.setFloorPrice(Integer.parseInt(floorPrice));
        }
        if (picUrl != null) {
            brand.setPicUrl(picUrl);
        }
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brandMapper.insertSelective(brand);
        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        BrandCreateVO brandCreateVO = new BrandCreateVO();
        brandCreateVO.setId(brand1.getId());
        brandCreateVO.setAddTime(String.valueOf(brand1.getAddTime()));
        brandCreateVO.setDesc(brand1.getDesc());
        brandCreateVO.setFloorPrice(brand1.getFloorPrice());
        brandCreateVO.setName(brand1.getName());
        brandCreateVO.setUpdateTime(String.valueOf(brand1.getUpdateTime()));
        return brandCreateVO;
    }

    @Override
    public Brand brandUpdate(Brand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKeySelective(brand);
        Brand returnBrand = brandMapper.selectByPrimaryKey(brand.getId());
        return returnBrand;
    }




    public IssueBean queryIssues(String question,Integer page, Integer limit, String sort, String order) {

        IssueExample issueExample = new IssueExample();
        //and + 列名 + 条件
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question != null){
            criteria.andQuestionLike("%" + question + "%");
        }
        //排序
        issueExample.setOrderByClause(sort + " " + order);
        List<Issue> issues = issueMapper.selectByExample(issueExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(issues);
        long total = pageInfo.getTotal();

        IssueBean issueBean = new IssueBean();

        issueBean.setTotal(total);
        issueBean.setItems(issues);

        return issueBean;
    }

    @Override
    public void create(Issue issue) {
        issue.setAddTime(new Date());
        issueMapper.insertSelective(issue);
    }

    @Override
    public void delete(Issue issue) {
        issueMapper.deleteByPrimaryKey(issue.getId());
    }

    @Override
    public void update(Issue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKey(issue);
    }

    @Override
    public ListBean queryKeyword(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        //排序
        PageHelper.startPage(page, limit);
        //提供条件
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if(keyword != null){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if(url != null){
            criteria.andUrlLike("%" + url + "%");
        }
        //排序
        keywordExample.setOrderByClause(sort+" "+order);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> pageInfo = new PageInfo<>(keywords);
        long total = pageInfo.getTotal();
        ListBean listBean = new ListBean();
        listBean.setItems(keywords);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public OrderDetailList orderDetail(Integer id) {
//        OrderExample orderExample = new OrderExample();
//        OrderExample.Criteria criteria = orderExample.createCriteria();

        Order order = orderMapper.selectByPrimaryKey(id);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria1 = orderGoodsExample.createCriteria();
        criteria1.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        User user = userMapper.selectByPrimaryKey(order.getUserId());
        UserVo userVo = new UserVo(user.getNickname(), user.getAvatar());
        OrderDetailList orderDetailList = new OrderDetailList();
        orderDetailList.setOrder(order);
        orderDetailList.setOrderGoods(orderGoods);
        orderDetailList.setUser(userVo);
        return orderDetailList;
    }

    @Override
    public void orderRefund(Integer orderId, Integer refundMoney) {
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(new Date());
        order.setOrderStatus((short) 203);
        int i = orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void updateOrderShipAndStatus(HashMap<String, Object> map) {
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        WxOrder wxOrder = new WxOrder();
        wxOrder.setOrderStatus((short) 301);
        wxOrder.setId(orderId);
        wxOrder.setShipChannel(shipChannel);
        wxOrder.setShipSn(shipSn);
        int i = wxOrderMapper.updateByPrimaryKeySelective(wxOrder);
    }

    @Override
    public void updateCommentReply(Map<String, Object> map) {
        Integer commentId = (Integer) map.get("commentId");
        String reply = (String) map.get("content");
        WxComment wxComment = wxCommentMapper.selectByPrimaryKey(commentId);
        String content = wxComment.getContent();
        content = content + " - " + reply;

        WxComment wxCommentReply = new WxComment();
        wxCommentReply.setId(commentId);
        wxCommentReply.setContent(content);
        wxCommentMapper.updateByPrimaryKeySelective(wxCommentReply);
    }

    public Keyword updateAndQueryKeyword(Keyword keyword) {
        keyword.setUpdateTime(new Date());
        int i = keywordMapper.updateKeyword(keyword);
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(keyword.getId());
        return keyword1;
    }

    @Override
    public Keyword createKeyword(Keyword keyword) {
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        int i = keywordMapper.insertSelective(keyword);
        int id = keywordMapper.selectLastInsertId();
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(id);
        return keyword1;
    }

    @Override
    public void deleteKeyword(Integer id) {
        int i = keywordMapper.deleteByPrimaryKey(id);
    }
}
